package com.liao.healthydiet.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.liao.healthydiet.ListViewFoodAboutLayoutAdapter;
import com.liao.healthydiet.R;
import com.liao.healthydiet.domain.About;
import com.liao.healthydiet.ui.adapter.ViewPagerFoodAboutAdapter;

import java.util.ArrayList;

public class FoodAboutActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, AdapterView.OnItemClickListener {

    private ViewPager viewPager;
    private LinearLayout layout_icon;
    private ListView listView;

    private ArrayList<View> views;
    private int[] images = new int[]{R.mipmap.ab1, R.mipmap.ab2, R.mipmap.ab3, R.mipmap.ab4, R.mipmap.ab5};
    private ViewPagerFoodAboutAdapter adapter;

    private ArrayList<ImageView> icons;

    private ArrayList<About> abouts;
    private ListViewFoodAboutLayoutAdapter listViewAdapter;
    private int[] listViewIcons = new int[]{R.mipmap.icon_door, R.mipmap.icon_phone, R.mipmap.icon_world, R.mipmap.icon_flash};
    private String[] listViewInfo = new String[]{"当前版本：v1.0", "联系电话：xxx-xxxxxxxx"
            , "公司地址：xxxxxx", "分享软件"};

    private int currItem = 0;
    private boolean isRun = true;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            viewPager.setCurrentItem(currItem);
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_about);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        initView();
        autoChange();
    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        layout_icon = (LinearLayout) findViewById(R.id.layout_icon);
        listView = (ListView) findViewById(R.id.listView);

        views = new ArrayList<>();
        icons = new ArrayList<>();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = 10;
        for (int i = 0; i < images.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setImageResource(images[i]);
            views.add(imageView);
            ImageView icon = new ImageView(this);
            icon.setLayoutParams(params);
            if (i == 0) {
                icon.setImageResource(R.mipmap.a3);
            } else {
                icon.setLayoutParams(params);
                icon.setImageResource(R.mipmap.a2);
            }
            layout_icon.addView(icon);
            icons.add(icon);
        }

        abouts = new ArrayList<>();
        for (int i = 0; i < listViewIcons.length; i++) {
            About about = new About();
            about.setIcon(listViewIcons[i]);
            about.setInfo(listViewInfo[i]);
            abouts.add(about);
        }
        listViewAdapter = new ListViewFoodAboutLayoutAdapter(this, abouts);
        listView.setAdapter(listViewAdapter);
        listView.setOnItemClickListener(this);

        adapter = new ViewPagerFoodAboutAdapter(views);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);
    }

    private void autoChange() {
        new Thread() {
            @Override
            public void run() {
                while (isRun) {
                    try {
                        sleep(1500);
                        handler.sendEmptyMessage(0);
                        int currentItem = viewPager.getCurrentItem();
                        currItem = currItem >= views.size() - 1 ? 0 : currentItem + 1;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }.start();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        for (int i = 0; i < icons.size(); i++) {
            if (i == position) {
                icons.get(i).setImageResource(R.mipmap.a3);
            } else {
                icons.get(i).setImageResource(R.mipmap.a2);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 3:
//                Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setType("text/plain");
                intent.putExtra(Intent.EXTRA_TITLE, "标题");
                intent.putExtra(Intent.EXTRA_TEXT, "内容");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(Intent.createChooser(intent, "分享"));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRun = false;
    }
}
